package com.shxc.smarttrade.community.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseFragment;
import com.shxc.smarttrade.currency.adapter.ViewPagerFragmentAdapter;
import com.shxc.smarttrade.main.MainActivity;
import com.shxc.smarttrade.utils.AppUtils;
import com.shxc.smarttrade.utils.Loger;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;


/**
 * 社区
 */
@ContentView(R.layout.fragment_community_a)
public class CommunityAndroidFragment extends BaseFragment {
    @ViewInject(R.id.quotation_button)
    TextView quotation_button;
    @ViewInject(R.id.market_button)
    TextView market_button;
    @ViewInject(R.id.master_button)
    TextView master_button;
    @ViewInject(R.id.question_button)
    TextView question_button;
    @ViewInject(R.id.viewpager)
    ViewPager viewpager;

    @ViewInject(R.id.mzbannerview)
    MZBannerView mzbannerview;
    @ViewInject(R.id.linearlayout)
    LinearLayout linearlayout;

    List<Fragment> fragmentList;

    //轮播图次序
    private int selected = 0;

    private List<Integer> bannerList ;
    MainActivity mainActivity;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        fragmentList  = new ArrayList<>();
        fragmentList.add(new AndroidQuotationFragment());
        fragmentList.add(new AndroidMarketFragment());
//        fragmentList.add(new AndroidMasterFragment());
        fragmentList.add(new AndroidQuestionFragment());

        viewpager.setOffscreenPageLimit(4);
        viewpager.setAdapter(new ViewPagerFragmentAdapter(getActivity().getSupportFragmentManager(), fragmentList));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setState(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mainActivity = (MainActivity) getActivity();
        mainActivity.jumpType = 2;

        initBanner();
    }

    private void initBanner() {

        //初始化banner
        mzbannerview.setIndicatorRes(R.color.white, R.color.black);
        mzbannerview.addPageChangeLisnter(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                selected = position;
                changeDotStatus();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bannerList = new ArrayList<>();
        bannerList.add(R.mipmap.sq_banner_1);
        bannerList.add(R.mipmap.sq_banner_2);
        initDots(bannerList.size());
        mzbannerview.setPages(bannerList, new MZHolderCreator<BannerPaddingViewHolder>() {
            @Override
            public BannerPaddingViewHolder createViewHolder() {
                return new BannerPaddingViewHolder();
            }
        });

        changeDotStatus();
    }

    /**
     * 初始化dot
     */
    private void initDots(int size) {
        linearlayout.removeAllViews();
        for (int i = 0; i < size; i++) {
            View view = new View(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AppUtils.dip2px(mContext, 5), AppUtils.dip2px(mContext, 5));
            params.setMargins(5, 0, 5, 0);
            view.setLayoutParams(params);
            view.setBackgroundResource(R.drawable.dot_selector_1);
            linearlayout.addView(view);
        }
    }

    private void bannerClick(int position) {
        switch (position){
            case 0:
                Loger.e("banner","0");
                break;
            case 1:
                Loger.e("banner","1");
                break;
        }
    }

    @Event({ R.id.right_textview, R.id.quotation_button, R.id.market_button, R.id.master_button, R.id.question_button})
    private void onClick(View view) {

        switch (view.getId()) {
//            case R.id.chat_imageview:
//                //聊天
//                if (SpUtils.getInt(mContext, "userId", -1) == -1) {
//                    ToToast("请先登录");
//                } else {
//                    String chartRoomId = SpUtils.getString(getActivity(), "chartRoomId");
//                    if (!TextUtils.isEmpty(chartRoomId))
//                        RongIM.getInstance().startConversation(getActivity(), Conversation.ConversationType.CHATROOM, chartRoomId, "聊天室");
//                    else
//                        BaseUtils.showToast("初始化失败", getActivity());
//                }
//                break;
            case R.id.quotation_button:
                //行情
                viewpager.setCurrentItem(0);
                break;
            case R.id.market_button:
                //市场
                viewpager.setCurrentItem(1);
                break;
            case R.id.master_button:
                //大师
//                viewpager.setCurrentItem(2);
                break;
            case R.id.question_button:
                //常见问题
                viewpager.setCurrentItem(2);
                break;
        }
    }

    public void setState(int state) {
        switch (state) {
            case 0:
                quotation_button.setTextColor(resources.getColor(R.color.quotation_blue));
                quotation_button.setBackgroundResource(R.drawable.box_community_pro);
                market_button.setTextColor(resources.getColor(R.color.quotation_gray));
                market_button.setBackgroundResource(R.drawable.box_community);
//                master_button.setTextColor(resources.getColor(R.color.quotation_gray));
//                master_button.setBackgroundResource(R.drawable.box_community);
                question_button.setTextColor(resources.getColor(R.color.quotation_gray));
                question_button.setBackgroundResource(R.drawable.box_community);
                break;
            case 1:
                quotation_button.setTextColor(resources.getColor(R.color.quotation_gray));
                quotation_button.setBackgroundResource(R.drawable.box_community);
                market_button.setTextColor(resources.getColor(R.color.quotation_blue));
                market_button.setBackgroundResource(R.drawable.box_community_pro);
//                master_button.setTextColor(resources.getColor(R.color.quotation_gray));
//                master_button.setBackgroundResource(R.drawable.box_community);
                question_button.setTextColor(resources.getColor(R.color.quotation_gray));
                question_button.setBackgroundResource(R.drawable.box_community);
                break;
//            case 2:
//                quotation_button.setTextColor(resources.getColor(R.color.quotation_gray));
//                quotation_button.setBackgroundResource(R.drawable.box_community);
//                market_button.setTextColor(resources.getColor(R.color.quotation_gray));
//                market_button.setBackgroundResource(R.drawable.box_community);
//                master_button.setTextColor(resources.getColor(R.color.quotation_blue));
//                master_button.setBackgroundResource(R.drawable.box_community_pro);
//                question_button.setTextColor(resources.getColor(R.color.quotation_gray));
//                question_button.setBackgroundResource(R.drawable.box_community);
//                break;
            case 2:
                quotation_button.setTextColor(resources.getColor(R.color.quotation_gray));
                quotation_button.setBackgroundResource(R.drawable.box_community);
                market_button.setTextColor(resources.getColor(R.color.quotation_gray));
                market_button.setBackgroundResource(R.drawable.box_community);
//                master_button.setTextColor(resources.getColor(R.color.quotation_gray));
//                master_button.setBackgroundResource(R.drawable.box_community);
                question_button.setTextColor(resources.getColor(R.color.quotation_blue));
                question_button.setBackgroundResource(R.drawable.box_community_pro);
                break;
        }
    }

    /**
     * 动态修改指示器的状态
     */
    private void changeDotStatus() {
        for (int i = 0; i < linearlayout.getChildCount(); i++) {
            linearlayout.getChildAt(i).setEnabled(i == selected);
        }
    }

    /**
     * 绑定banner图
     */
    public class BannerPaddingViewHolder implements MZViewHolder<Integer> {
        private ImageView banner_imageview;

        @Override
        public View createView(Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item_padding, null);
            banner_imageview =  view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, final int position, Integer data) {
            // 数据绑定
            banner_imageview.setImageResource(bannerList.get(position));
            banner_imageview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bannerClick(position);
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mzbannerview.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        mzbannerview.pause();
    }
}
